package com.mv.webserver.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mv.webserver.conf.Config;
import com.mv.webserver.http.EmptyRequestException;
import com.mv.webserver.http.HttpRequest;
import com.mv.webserver.http.HttpResponse;
import com.mv.webserver.http.HttpServlet;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler implements Runnable{
    private static final Logger log = LoggerFactory.getLogger(ClientHandler.class);
    private Socket socket;
    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
    	do {
    		
    	}while(false);
        try {
        	
            //1.解析请求
            //实例化就是解析过程，得到的对象就表示本次请求的内容
            HttpRequest request = new HttpRequest(socket);
            HttpResponse response = new HttpResponse(socket);

            String path = request.getRequestURI();
            log.info("抽象路径:"+path);
            
            do {
            	//servelet非空 核心分发器请求
            	HttpServlet servlet = ServletContext.getServlet(Config.DISPATCHER_SERVLET); //检查是否存在前端分发器 先执行
                if(servlet != null) {
                	log.info(" 核心分发器请求");
                	servlet.service(request,response);
                	break;
                }
                
              //servelet非空请求
                servlet = ServletContext.getServlet(path);
                if (servlet != null) {
                	log.info("servlet列表匹配 路径:" + path);
                	if (request.isGet()) {
                		servlet.doGet(request,response);
                    }else if (request.isPost()) {
                    	servlet.doPost(request,response);
                    }else if (request.isUpdate()) {
                    	servlet.doUpdate(request,response);
                    }else if (request.isDelete()) {
                    	servlet.doDelete(request,response);
                    }else {
                    	servlet.service(request,response);
                    }
                	break;
                }
              
                //资源文件或其它请求
                File file = new File("webapps"+path);
            	if (file.isFile() && file.exists()) {
            	    log.info("资源已找到!");
            	    response.setEntity(file);
            	} else {
            	    log.info("资源不存在!");
            	    response.setStateCode(404);
            	    response.setStateReason("NotFound");
            	    response.setEntity(new File("webapps/root/404.html"));
            	}
            }while(false);
            
            //3.响应客户端
            response.flush();
            log.info("响应客户端完毕!");
        } catch (EmptyRequestException e) {
            log.error("空请求...");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        } finally {
            try {
                //与客户端断开连接  实现HTTP短连接
                socket.close();
            } catch (IOException e) {
                log.error(e.getMessage(),e);
            }
        }
    }
}